<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>密码生成器</title>
    <style>
    html, body {
        margin: 0;
        padding: 0;
    }

    .container {
        padding: 10px 15px;
        font-size: 16px;
    }

    #len {
        border: 1px solid #999;
        padding: 4px;
    }

    #create {
        font-size: 14px;
        padding: 2px 6px;
        background: #43ac43;
        color: #fff;
    }

    #output {
        color: #333;
        line-height: 1.6;
        margin-top: 2em;
        user-select: text;
    }
    </style>
</head>
<body>
    <div class="container">
        <div>
            <label>
                密码长度：<input type="number" placeholder="输入密码长度" id="len" value="15">
            </label>
            <button id="create">创建</button>
        </div>
        <div id="output">
        </div>
    </div>

    <script>
        init();

        function $(el) {
            return document.querySelector(el);
        }

        function init() {
            $('#create').addEventListener('click', onClick);
            createPassword();
        }

        function onClick() {
            createPassword();
        }

        function createPassword() {
            const len = Number($('#len').value);
            const passwd = getPasswd(len);
            $('#output').textContent = passwd;
        }

        function getPasswd(len) {
            let result = '';
            for (let i = 0; i < len; i++) {
               result += getRandomChar(); 
            }

            return result;
        }

        function getRandomChar() {
            const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-*/@#$&?';
            const len = chars.length;
            const idx = Math.floor(Math.random() * len);
            return chars.charAt(idx);
        }
    </script>
</body>
</html>
